<?php
/**
 * 
 * @author Freezy
 *
 */
class Nilai extends MY_Controller {

	function Nilai(){
		parent::MY_Controller();
		$this->validate_cookie();
	}
	
	function index(){
		$this->load->helper(array('html'));
		$this->load->config('rnd', true);
		$CONF = $this->config->item('rnd');

		//layout first
		$layoutConf = $CONF['layout'];
		$layoutConf['left_content'] = array('view'=>'template/horz_nav', 
											'view_data'=>array('data'=>$CONF['nav']['psdm']));
		$layoutConf['right_content'] = array('view'=>'nilai/overview', 
											'view_data'=>$CONF['search']);
		$this->initialize_layout($layoutConf);
		
		
		//page
		$pageConf = $CONF['page'];
		$pageConf['jscript_default'][] = 'psdm/nilai';
		$pageConf['jscript_default'][] = 'utils/searchDialog';
		$pageConf['jscript'] = $pageConf['jscript_default'];
		$pageConf['css'] = $pageConf['css_default'];
		$pageConf['template_view'] = 'template/2_layout';
		$this->initialize_page($pageConf);
		
		$this->finalize_view();		
	}
	
	function _search(){
		$this->load->model('nilai_model');
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		$conf1 = $conf['achievement']['jenis_options'];
		$conf2 = array_merge($conf['detachievement']['tingkat_options']['PR'],$conf['detachievement']['tingkat_options']['PE']);
		
		//ada kodenya, jadi load config dulu
		$key = $this->input->post('key');
		$keysss = array();
		$keysss[] = $key; 
		if ($ss = array_search(ucwords($key), $conf1)) $keysss[] = $ss;	//key untuk jenis
		if ($ss = array_search(ucwords($key), $conf2)) $keysss[] = $ss;	//key untuk jenis
		
		$limit = $this->input->post('limit');
		$offset = $this->input->post('offset') * $limit;
		$orderby = $this->input->post('orderby');
		
		$table_content = "";
		$row = 0;
		$data = $this->nilai_model->select_last_entry($keysss, false, $limit, $offset, $orderby);
		$table_content = $this->load->view('nilai/table_content', array(
			'data'=>$data,
			'jenis_options'=>$conf1,
			'tingkat_options'=>$conf2
		), true); 	
		echo json_encode(array(
			'table_content'=>$table_content,
			'table_num_record'=> $key? $data->num_rows() : $this->nilai_model->count_record()
		));
	}
	
	function _addModal($detachiev_id=false, $reward_id=false){
		$this->load->helper(array('html','url'));
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$this->load->model('detachievement_model');
		$this->load->model('rewardPunishment_model');
		$this->finalize_view_str(
			$this->load->view('nilai/add', array(
				'detachiev_data'=> (array) $this->detachievement_model->select_by_id($detachiev_id? $detachiev_id : "")->row(),
				'detachiev_tingkat'=>array_merge($conf['detachievement']['tingkat_options']['PR'],$conf['detachievement']['tingkat_options']['PE']),
				'detachiev_jenis'=>$conf['achievement']['jenis_options'],
				'reward_data'=>(array) $this->rewardPunishment_model->select_by_id($reward_id? $reward_id : "")->row(),
				'reward_options'=>$conf['rewardPunishment']['jenis_options']
			),true)
		);
	}
	
	function _save(){
		$this->load->model('nilai_model');
		$inp = $this->input->post('inp');
		
		$fields = array('nilai_point','nilai_min', 'nilai_max', 'detachiev_id', 'reward_id');
		$values = array($inp[4], $inp[2], $inp[3], $inp[0], $inp[1]);
		$data = array_combine($fields,$values);
		
		//check range
		$check = $this->nilai_model->select_range($data['detachiev_id'], $data['nilai_min'], $data['nilai_max']);
		if ($check->num_rows()>0){
			$check = (array)$check->row();
			$res = false;
			$msg = 'Range nilai konflik dengan KODE "'.$check['nilai_id'] .'"';
		}
		else{
			$res = $this->nilai_model->insert( $data );
			$msg = $this->nilai_model->mysql_innodb_error(mysql_errno()); 
		}
		$msg = $res? "Proses simpan selesai" : "Proses simpan gagal, $msg";
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _editModal($id){
		$this->load->model('nilai_model');
		$this->load->config('rnd',true);
		$conf = $this->config->item('rnd');
		
		$result = $this->nilai_model->select_by_id($id);
		$result = (array)$result->row();
		
		$this->load->model('detachievement_model');
		$this->load->model('rewardPunishment_model');
		$arr = array(
			//'detachiev_data'=> (array) $this->detachievement_model->select_by_id($result['detachiev_id'])->row(),
			'tingkat_options'=>	array_merge($conf['detachievement']['tingkat_options']['PR'],$conf['detachievement']['tingkat_options']['PE']),
			'jenis_options'=>$conf['achievement']['jenis_options'],
			//'reward_data'=>(array) $this->rewardPunishment_model->select_by_id($result['reward_id'])->row(),
			'reward_options'=>$conf['rewardPunishment']['jenis_options']
		);
		
		$this->finalize_view_str(
			$this->load->view('nilai/edit', array_merge($result, $arr), true)
		);
	}
	
	function _update(){
		$this->load->model('nilai_model');
		$inp = $this->input->post('inp');
		
		$fields = array('nilai_point','nilai_min', 'nilai_max','detachiev_id');
		$values = array($inp[4], $inp[2], $inp[3], $inp[0]);
		$data = array_combine($fields,$values);
		
		//check range
		$check = $this->nilai_model->select_range($data['detachiev_id'], $data['nilai_min'], $data['nilai_max']);
		if ($check->num_rows()>0){
			$check = (array)$check->row();
			$res = false;
			$msg = 'Range nilai konflik dengan KODE "'.$check['nilai_id'] .'"';
		}
		else{
			$res = $this->nilai_model->update(array('nilai_id' => $inp[5]), $data);
			$msg = $this->nilai_model->mysql_innodb_error(mysql_errno()); 
		}
		
		$msg = $res? "Proses update selesai" : "Proses update gagal, ".$this->nilai_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	function _delete($id){
		$this->load->model('nilai_model');
		$res = $this->nilai_model->delete(array('nilai_id' => $id));
		$msg = $res? "Proses hapus selesai" : "Proses hapus gagal, ".$this->detachievement_model->mysql_innodb_error(mysql_errno());
		echo json_encode(array('error'=>!$res,'msg'=>$msg));
	}
	
	/*****************************************
	 * MISC
	 *****************************************/
	function _getComboBidang($jenis, $selected_id=false, $return=false){
		$this->load->model('achievement_model');
		$dd = $this->achievement_model->select_by_jenis($jenis, array('achiev_bidang'=>'asc'));
		$cmb = "";
		foreach($dd->result_array() as $row){
			$selected = $selected_id==$row['achiev_id']? "selected" : "";
			$cmb .= "<option value=\"". $row['achiev_id'] ."\" $selected>". $row['achiev_bidang'] ."</option>";
		}
		if ($return) return $cmb;
		 
		$this->finalize_view_str($cmb);
	}
	
	function _validateRange($detachiev_id, $value, $nilai_id = false){
		$this->load->model('nilai_model');
		$check = $this->nilai_model->check_range($detachiev_id, $value, $nilai_id);
		if ($check = (array)$check->row()){
			echo json_encode(array('error'=>true,'msg'=>"Range nilai konflik dengan KODE '".$check['nilai_id']."'"));
		}
		else{
			echo json_encode(array('error'=>false,'msg'=>""));
		}
	}
	
	function _validateAmmount($min, $max){
		if ($min>$max){
			echo json_encode(array('error'=>true,'msg'=>"Nilai max harus lebih dari Nilai min"));
		}
		else{
			echo json_encode(array('error'=>false,'msg'=>""));
		}
	}
}
/* End of file psdm/nilai.php */
/* Location: ./system/application/controllers/psdm/nilai.php */